package com.zhz.leetcode;

import java.util.Arrays;

/**
 * 26. 删除有序数组中的重复项
 * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
 */
public class RemoveDuplicates26 {

    public static void main(String[] args) {
//        输入：nums = [0,0,1,1,1,2,2,3,3,4]
//        输出：5, nums = [0,1,2,3,4]

        RemoveDuplicates26 m = new RemoveDuplicates26();
        int[] nums = {0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 4,5,5,5,5};
        int a = m.removeDuplicates1(nums);
        System.out.println("结果：" + Arrays.toString(nums));
        System.out.println("长度: " + a);
    }


    /**
     * 因为时有序的，所以只需要每个数字都只需要记录第一次出现，不需要考虑跳跃出现的问题
     */
    public int removeDuplicates(int[] nums) {
        int tmp = 0;
        int index = 0; //第一次出现的时候
        for (int i = 0; i < nums.length; i++) {
            if (i == 0) {
                tmp = nums[0];
            } else {
                if (tmp != nums[i]) {
                    nums[index + 1] = nums[i];
                    tmp = nums[i];
                    index += 1;
                }
            }
        }
        for (int j = index + 1; j < nums.length; j++) {
            nums[j] = 0;
        }
        return index + 1;
    }


    public int removeDuplicates1(int[] nums) {
        int slow = 0, fast = 0;
        while (fast < nums.length) {
            if (nums[slow] != nums[fast]) {
                nums[++slow] = nums[fast];
            }
            fast++;
        }
        return slow+1;
    }

}
